package com.softtown.modules.message.controller;

import com.qcloud.cos.utils.StringUtils;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.message.service.SysEmailService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: EmailController
 * @Description: 邮箱发送Controller层
 * @Author: fuming
 * @Date: 2019/7/11 9:23
 */
@RestController
public class EmailController {

    @Resource
    private SysEmailService sysEmailService;

    /**
     * @Title: sendEmail
     * @Description: 邮箱发送验证码快捷登陆
     * @param @param [emailAddress]
     * @return @return com.softtown.common.utils.Result
     * @author Dingyayong
     * @throws
     * @date 2019/7/12 17:48
     */
    @PostMapping("/email")
    @ApiOperation(value = "邮箱发送验证码快捷登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emailAddress", value = "邮箱号", paramType = "query", required = true, dataType = "String"),
    })
    public Result sendEmail(@RequestBody String emailAddress) {
        Result result = new Result<>();
        String emailAddr = JSONObject.fromObject(emailAddress).get("emailAddress").toString();
        if (StringUtils.isNullOrEmpty(emailAddr)) {
            return result.error(ErrorCode.EMAIL_EMPTY);
        }
        if (!RegularUtils.isEmail(emailAddr)) {
            return result.error(ErrorCode.INCORRECT_EMAIL_FORMAT);
        }
        boolean flag = sysEmailService.sendEmailVerCode(emailAddr);
        if(flag){
            return result;
        }
        return result.error(ErrorCode.EMAIL_FAIL_SEND);
    }

    /**
     * @Title: sendEmailByRest
     * @Description: 邮箱发送验证码找回重置
     * @param @param [emailAddress]
     * @return @return com.softtown.common.utils.Result
     * @author Dingyayong
     * @throws
     * @date 2019/7/12 17:48
     */
    @PostMapping("/sendEmailByRest")
    @ApiOperation(value = "邮箱发送验证码找回重置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emailAddress", value = "邮箱号", paramType = "query", required = true, dataType = "String"),
    })
    public Result sendEmailByRest(@RequestBody String emailAddress) {
        Result result = new Result<>();
        String emailAddr = JSONObject.fromObject(emailAddress).get("emailAddress").toString();
        if (StringUtils.isNullOrEmpty(emailAddr)) {
            return result.error(ErrorCode.EMAIL_EMPTY);
        }
        if (!RegularUtils.isEmail(emailAddr)) {
            return result.error(ErrorCode.INCORRECT_EMAIL_FORMAT);
        }
        boolean flag = sysEmailService.sendEmailByRest(emailAddr);
        if(flag){
            return result;
        }
        return result.error(ErrorCode.EMAIL_FAIL_SEND);
    }

    /**
     * @Title: sendEmailByRest
     * @Description: 邮箱发送验证码绑定邮箱
     * @param @param [emailAddress]
     * @return @return com.softtown.common.utils.Result
     * @author Dingyayong
     * @throws
     * @date 2019/7/12 17:48
     */
    @PostMapping("/sendEmailByEmail")
    @ApiOperation(value = "邮箱发送验证码绑定邮箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emailAddress", value = "邮箱号", paramType = "query", required = true, dataType = "String"),
    })
    public Result sendEmailByEmail(@RequestBody String emailAddress) {
        Result result = new Result<>();
        String emailAddr = JSONObject.fromObject(emailAddress).get("emailAddress").toString();
        if (StringUtils.isNullOrEmpty(emailAddr)) {
            return result.error(ErrorCode.EMAIL_EMPTY);
        }
        if (!RegularUtils.isEmail(emailAddr)) {
            return result.error(ErrorCode.INCORRECT_EMAIL_FORMAT);
        }
        boolean flag = sysEmailService.sendEmailByEmail(emailAddr);
        if(flag){
            return result;
        }
        return result.error(ErrorCode.EMAIL_FAIL_SEND);
    }

    /**
     * @Title: sendEmailByRest
     * @Description: 邮箱发送验证码绑定新邮箱
     * @param @param [emailAddress]
     * @return @return com.softtown.common.utils.Result
     * @author Dingyayong
     * @throws
     * @date 2019/7/12 17:48
     */
    @PostMapping("/sendEmailByNewEmail")
    @ApiOperation(value = "邮箱发送验证码绑定新邮箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emailAddress", value = "邮箱号", paramType = "query", required = true, dataType = "String"),
    })
    public Result sendEmailByNewEmail(@RequestBody String emailAddress) {
        Result result = new Result<>();
        String emailAddr = JSONObject.fromObject(emailAddress).get("emailAddress").toString();
        String username = JSONObject.fromObject(emailAddress).get("username").toString();
        if (StringUtils.isNullOrEmpty(emailAddr)) {
            return result.error(ErrorCode.EMAIL_EMPTY);
        }
        if (!RegularUtils.isEmail(emailAddr)) {
            return result.error(ErrorCode.INCORRECT_EMAIL_FORMAT);
        }
        boolean flag = sysEmailService.sendEmailByNewEmail(emailAddr,username);
        if(flag){
            return result;
        }

        return result.error(ErrorCode.EMAIL_FAIL_SEND);
    }

    /**
     * @Title: sendEmailByRegister
     * @Description: 邮箱发送验证码用户注册【待定】
     * @param @param [emailAddress]
     * @return @return com.softtown.common.utils.Result
     * @author Dingyayong
     * @throws
     * @date 2019/7/12 17:48
     */
    @PostMapping("/sendEmailByRegister")
    @ApiOperation(value = "邮箱发送验证码用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "emailAddress", value = "邮箱号", paramType = "query", required = true, dataType = "String"),
    })
    public Result sendEmailByRegister(@RequestBody String emailAddress) {
        Result result = new Result<>();
        String emailAddr = JSONObject.fromObject(emailAddress).get("emailAddress").toString();
        if (StringUtils.isNullOrEmpty(emailAddr)) {
            return result.error(ErrorCode.EMAIL_EMPTY);
        }
        if (!RegularUtils.isEmail(emailAddr)) {
            return result.error(ErrorCode.INCORRECT_EMAIL_FORMAT);
        }
        boolean flag = sysEmailService.sendEmailByRegister(emailAddr);
        if(flag){
            return result;
        }
        return result.error(ErrorCode.EMAIL_FAIL_SEND);
    }
}
